package com.yxqz.scz.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 鱼香茄子
 * @data 2021/10/26
 * 路由模式测试
 */
public class Producer03_routing {
    //    创建队列 邮件 短信，交换机
    private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    private static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    private static final String EXCHANGE_ROUTING_INFORM="exchange_routing_inform";
    private static final String ROUTING_KEY_EMAL="key_email";
    private static final String ROUTING_KEY_SMS="key_sms";


    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”，虚拟机相当于一个独立的mq服务器
            //创建与RabbitMQ服务的TCP连接
            connection = factory.newConnection();
            //创建与Exchange的通道，每个连接可以创建多个通道，每个通道代表一个会话任务
            channel = connection.createChannel();
            /**
             * 声明队列，如果Rabbit中没有此队列将自动创建
             * param1:队列名称
             * param2:是否持久化
             * param3:队列是否独占此连接
             * param4:队列不再使用时是否自动删除此队列
             * param5:队列参数
             */
            channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
            channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
            /* 声明一个交换机
            参数明细
                    * 1、交换机名称
                    * 2、交换机类型，fanout、topic、direct、headers*/
            channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, BuiltinExchangeType.DIRECT);


            /* 交换机和队列进行绑定
             * 参数明细：
             * 1、队列名称
             * 2、交换机名称
             * 3、路由key 交换机通过key值把消息发送到指定的 队列中。
             */
            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_ROUTING_INFORM,ROUTING_KEY_EMAL);
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_ROUTING_INFORM,ROUTING_KEY_SMS);
//            超级vip路由key都可以获取到
            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_ROUTING_INFORM,"supervip");
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_ROUTING_INFORM,"supervip");



//            发送消息
            for (int i = 0 ; i<3;i++){
//                指定路由关键字
                String message = "我爱吃鱼香茄子"+i;
                channel.basicPublish(EXCHANGE_ROUTING_INFORM,ROUTING_KEY_EMAL, null, message.getBytes());
                System.out.println("发送的 Message is:'" + message + "'");
            }
//            发送消息
            for (int i = 0 ; i<10;i++){
//                指定路由关键字
                String message = "我爱吃鱼香茄子"+i;
                channel.basicPublish(EXCHANGE_ROUTING_INFORM,ROUTING_KEY_SMS, null, message.getBytes());
                System.out.println("发送的 Message is:'" + message + "'");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }


}
